import mongoose from "mongoose";

const start = () => {
  // MongoDB / system 数据库连接字符串
  const mongoUri = `${process.env.MONGO_URL}/system`;

  // 连接到 MongoDB
  const connectToDatabase = async () => {
    try {
      await mongoose.connect(mongoUri, {});
    } catch (error) {
      console.error("链接 Mongoose 出错了:", error);
    }
  };

  // 处理连接状态
  mongoose.connection.on("connected", () => {
    console.log("链接 Mongoose/system 数据库成功");
  });

  mongoose.connection.on("error", (err) => {
    console.error("链接 Mongoose/system 数据库出错了:", err);
  });

  mongoose.connection.on("disconnected", () => {
    console.log("链接 Mongoose/system 数据库失败");
  });

  // 处理应用终止
  process.on("SIGINT", async () => {
    await mongoose.connection.close();
    console.log("关闭 Mongoose/system 数据库链接");
    process.exit(0);
  });

  // 调用连接函数
  connectToDatabase();
};

export default {
  start,
};
